package com.jie.controller;


import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jie.dao.GoodsDao;
import com.jie.entity.Evaluation;
import com.jie.entity.Goods;
import com.jie.entity.Shop;
import com.jie.service.EvaluationService;
import com.jie.service.GoodsService;
import com.jie.service.ShopService;
import com.jie.utility.R;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jie
 * @since 2024-03-26
 */
@Transactional
@RestController
@RequestMapping("/rear-end")
public class EvaluationController {
    @Autowired
    private EvaluationService evaluationService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private RestHighLevelClient client;

    /**
     * 保存评价
     * @param evaluation
     * @return
     */
    @PostMapping("/saveEvca")
    public R<String> saveEvca(@RequestBody Evaluation evaluation) {
        boolean save = evaluationService.save(evaluation);
        if(save){
            Goods goods = goodsService.getById(evaluation.getGoodsId());
            int star= (int) ((goods.getStar()+evaluation.getGoodsEvaluation())/2);
            goods.setStar(Integer.valueOf(star));
            UpdateRequest request = new UpdateRequest("goods", goods.getGoodsId());
            // 使用 XContentBuilder 构建要更新的数据
            XContentBuilder builder = null;
            try {

                    builder = XContentFactory.jsonBuilder();
                    builder.startObject()
                            .field("star", star);
                    builder.endObject();
                    request.doc(builder);
                    client.update(request, RequestOptions.DEFAULT);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            boolean b = goodsService.updateById(goods);
            if(b){
                LambdaQueryWrapper<Shop> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(Shop::getCreateAdmin,evaluation.getShopId());
                Shop shop = shopService.getOne(queryWrapper);
                String s= String.format("%.1f",((shop.getSeller()+evaluation.getSeller())/2));
                String m= String.format("%.1f",((shop.getMaterialFlow()+evaluation.getMaterialFlow())/2));
                String e= String.format("%.1f",((shop.getTreasure()+evaluation.getTreasure())/2));
                shop.setSeller(Double.valueOf(s));
                shop.setMaterialFlow(Double.valueOf(m));
                shop.setTreasure(Double.valueOf(e));
                boolean b1 = shopService.updateById(shop);
                if(b1)return R.success("评价成功");
            }

        }
        return R.error("评价失败");
    }

    /**
     * 获取评价
     * @param evaluation
     * @return
     */
    @GetMapping("/getEva")
    public R<List<Evaluation>> getEva(Evaluation evaluation){
        LambdaQueryWrapper<Evaluation> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(evaluation.getGoodsId()),Evaluation::getGoodsId,evaluation.getGoodsId());
        queryWrapper.eq((evaluation.getUserId()!=null&&evaluation.getUserId()!=0),Evaluation::getUserId,evaluation.getUserId());
        List<Evaluation> list = evaluationService.list(queryWrapper);
        return R.success(list);
    }

}

